package demo11;

import java.util.ArrayList;
import java.util.List;

/**
 * 二叉树的所有路径
 * https://leetcode.cn/problems/binary-tree-paths/
 */
class Solution1 {
    List<String> ret = new ArrayList<>();
    public List<String> binaryTreePaths(TreeNode root) {
        dfs(root,new StringBuffer());
        return ret;
    }

    private void dfs(TreeNode root, StringBuffer _path) {
        StringBuffer path = new StringBuffer(_path);
        // 出口
        if(root.left == null && root.right == null) {
            path.append(root.val);
            ret.add(String.valueOf(path));
            return;
        }
        path.append(root.val + "->");
        if(root.left != null) dfs(root.left, path);
        if(root.right != null) dfs(root.right, path);
        return;
    }
}